package com.atguigu.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.*;
import com.atguigu.gulimall.product.vo.AttrGroupRelationVo;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * ÊôÐÔ·Ö×é
 *
 * @author guokefa
 * @email guokefa@gmail.com
 * @date 2022-02-09 16:41:09
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 获取分类下所有分组&关联属性
     * /product/attrgroup/{catelogId}/withattr
     */
    @GetMapping("{catelogId}/withattr")
    public R attrGroupWithAttr(@PathVariable("catelogId") Long cateId){
        List<AttrGroupWithAttrVo> vos = attrAttrgroupRelationService.getAttrGroupWithAttr(cateId);

        return R.ok().put("data",vos);
    }

    ///product/attrgroup/{attrgroupId}/noattr/relation
    @GetMapping("{attrgroupId}/noattr/relation")
    public R noattrRelation(
            @RequestParam Map<String, Object> params,
            @PathVariable("attrgroupId") Long attrgroupId
    ){
        PageUtils page = attrGroupService.noattrRelation(params, attrgroupId);

        return R.ok().put("page",page);
    }

    /**
     * 删除组的关联规格属性
     *
     * @param attrGroupRelationVo
     * @return
     */
    ///product/attrgroup/attr/relation/delete
    @PostMapping("attr/relation/delete")
    public R deletedAttrGroupRelation(@RequestBody AttrGroupRelationVo[] attrGroupRelationVo){
        attrGroupService.deletedAttrGroupRelation(attrGroupRelationVo);

        return R.ok();
    }

    ///product/attrgroup/attr/relation
    /**
     * 添加属性与分组关联关系
     */
    @PostMapping("/attr/relation")
    //@RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody AttrAttrgroupRelationEntity[] attrAttrgroupRelationEntity){
//		categoryBrandRelationService.save(categoryBrandRelation);
        System.out.println(attrAttrgroupRelationEntity);
//        attrAttrgroupRelationService.addRelation(vos);
        attrAttrgroupRelationService.saveDetail(attrAttrgroupRelationEntity);

        return R.ok();
    }


    /**
     *  分组关联属性
     *
     * @param attrGroupId
     * @return
     */
    ///product/attrgroup/{attrgroupId}/attr/relation
    @GetMapping("{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrGroupId){
        List<AttrEntity> attrList = attrService.queryRelationAttr(attrGroupId);
        return R.ok().put("data",attrList);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{categoryId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("categoryId") Long categoryId){
        PageUtils page = attrGroupService.queryBaseAttrPage(params,categoryId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        //获取完整的三级路径
        Long[] path = categoryService.getCategoryPath(catelogId);
        attrGroup.setCategoryPath(path);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
